package com.axy.imperfectblog.service;

import com.axy.imperfectblog.entity.User;
import com.axy.imperfectblog.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;


    private PasswordEncoder passwordEncoder;
    public User registerUser(String username, String password, String email){
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setEmail(email);
        return userRepository.save(user);
    }

    public User loginUser(String username, String password){
        User user = userRepository.findByUsername(username);
        if(user != null && passwordEncoder.matches(password,user.getPassword())){
            return user;
        }
        return null;
    }
}
